<?php


use Illuminate\Support\Env;

// $num为生成汉字的数量
function getChar($num)
{
    $str = '';
    for($i = 0; $i < $num; $i++){
        // 使用chr()函数拼接双字节汉字，前一个chr()为高位字节，后一个为低位字节
        $a   = chr(mt_rand(0xB0, 0xD0)).chr(mt_rand(0xA1, 0xF0));
        $str .= iconv('GB2312', 'UTF-8', $a); // 转码
    }

    return $str;
}

// 伪静态链接
function htmlUrl($path = null, $parameters = [])
{
    return url($path.'.html', $parameters);
}

// 翻页链接
function pageUrl($url, $page = 0)
{
    $url = url($url);
    if($page){
        if(strpos($url, '?') === false){
            $url = $url.'?page='.$page;
        } else{
            $url = $url.'&page='.$page;
        }
    }

    return $url;
}

// 匿名展示
function anony($user_name)
{
    $strlen   = mb_strlen($user_name, 'utf-8'); //获取字符长度
    $firstStr = mb_substr($user_name, 0, 1, 'utf-8');  //查找字符第一个
    $str      = $firstStr.str_repeat('*', $strlen - 1);  //拼接第一个+把字符串 "* " 重复 $strlen - 1 次：

    return $str;
}

// 生成随机字符串
function random($num = 4)
{
    // 去掉了 0 1 O l 等
    $str   = "23456789abcdefghijkmnpqrstuvwxyz";  //ABCDEFGHIJKLMNPQRSTUVW 不要在写的
    $str   = "1234567890";  //ABCDEFGHIJKLMNPQRSTUVW 不要在写的
    $code  = '';
    $lenth = strlen($str);
    for($i = 0; $i < $num; $i++){
        $code .= $str[mt_rand(0, $lenth - 1)];
    }

    return $code;
}

// 获取默认图片
function getRealImage($img)
{
    $img = $img ?: Env::get('STYLE_HOST').'src/img/default.ico';

    return $img;
}

// 时间展示
function htmlDate($timestamp)
{
    return date('Y-m-d H:i', $timestamp);

    /*$timeAgo = LARAVEL_TIME - $timestamp;
    if($timeAgo > 86400){
        return floor($timeAgo / 86400).'天前';
        //return date('Y-m-d', $timestamp);
    } else{
        if($timeAgo > 3600){
            return floor($timeAgo / 3600).'小时前';
        } else{
            return floor($timeAgo / 60).'分钟前';
        }
        //return date('Y-m-d H:i', $timestamp);
    }*/
}

// 样式表
function sass($path, $type = 'pc')
{
    $type  = $type == 'pc' ? 'pc' : 'wap';
    $ds    = DIRECTORY_SEPARATOR;
    $base  = app()->basePath().$ds;
    $style = $base.'public'.$ds.'static'.$ds.$type.$ds;
    $form  = $base.$path;
    $file  = basename($form);
    $to    = $style.$file;
    if(!file_exists($to) || (filemtime($form) > @filemtime($to))){
        $data = file_get_contents($form);
        file_put_contents($to, $data);
    }

    echo '<link type="text/css" href="/static/'.$type.'/'.$file.'" rel="stylesheet" />';
}

// js
function scripts($path, $type = 'pc')
{
    $type  = $type == 'pc' ? 'pc' : 'wap';
    $ds    = DIRECTORY_SEPARATOR;
    $base  = app()->basePath().$ds;
    $style = $base.'public'.$ds.'static'.$ds.$type.$ds;
    $form  = $base.$path;
    $file  = basename($form);
    $to    = $style.$file;
    if(!file_exists($to) || (filemtime($form) > @filemtime($to))){
        $data = file_get_contents($form);
        file_put_contents($to, $data);
    }

    echo '<script type="text/javascript" src="/static/'.$type.'/'.$file.'"></script>';
}

// 替换表情
function _replaceFace($content)
{
    $domain  = Env::get('STYLE_HOST');
    $content = preg_replace('/\[\/表情([0-9]*)\]/si', '<img src="'.$domain.'static/face/$1.gif" border="0" />', $content);


    return $content;
}


//从内容中取出图片
function _getimg4content($content)
{
    //preg_match_all('/<[\s]*img.*?src=[\'|"|\s]*(.+?|\ )[\'|"|\s|>]/is', $content, $matches);
    preg_match_all('/<[\s]*[img|IMG].*?src=[\'|"|\s]*(.+?|\ )[\'|"|\s|>]/is', $content, $matches);
    $aryimg = array();
    for($j = 0; $j < count($matches[1]); $j++){
        $aryimg[] = $matches[1][$j];
    }

    return $aryimg; //$matches[1][$j][0] //返回第一个匹配的图
}

//微博中@字符替换为相关的用户姓名
function _replaceAlt($str)
{
    $old_str  = $str;
    $_keyword = [];

    // 如果有 @ 在替换
    if(strstr($old_str, '@') <> ""){
        $nstr = explode('@', $str);
        for($ii = 1; $ii < count($nstr); $ii++){
            $key = explode(' ', $str[$ii]);
            $key = $key[0];

            //去除相同的@
            if(!in_array($key, $_keyword)){
                $_keyword[] = $key;
            }
        }

        // 组合查询数据
        $userDb = new \App\Models\User();
        $field  = ['id', 'username', 'password', 'email', 'status', 'mobile', 'realname',]; //, 'salt'
        $lists  = $userDb->whereIn('username', $_keyword)->select($field)->get()->toArray();
        foreach($lists as $item){
            $key       = $item['username'];
            $realName  = $item['realname'] ?? $item['username'];
            $sXingming = "<a href='javascript:;' class='username search' onclick='search_right(\"{$key}\")' title='(@{$key})'>@".$realName.'</a>';
            $old_str   = str_replace('@'.$key, $sXingming, $old_str);
        }
    }


    return $old_str;
}


//清除HTML标记
function _clearhtml($str)
{
    $str = preg_replace("/\s+/", " ", $str); //过滤多余回车
    $str = preg_replace("/<[ ]+/si", "<", $str); //过滤<__("<"号后面带空格)

    $str = preg_replace("/<\!--.*?-->/si", "", $str); //注释
    $str = preg_replace("/<(\!.*?)>/si", "", $str); //过滤DOCTYPE
    $str = preg_replace("/<(\/?html.*?)>/si", "", $str); //过滤html标签
    $str = preg_replace("/<(\/?script.*?)>/si", "", $str); //过滤html标签
    $str = preg_replace("/<(\/?head.*?)>/si", "", $str); //过滤head标签
    $str = preg_replace("/<(\/?meta.*?)>/si", "", $str); //过滤meta标签
    $str = preg_replace("/<(\/?body.*?)>/si", "", $str); //过滤body标签
    $str = preg_replace("/<(\/?link.*?)>/si", "", $str); //过滤link标签
    $str = preg_replace("/<(\/?form.*?)>/si", "", $str); //过滤form标签
    $str = preg_replace("/(<img.*?>)(.*?)(<\/img>)/si", "", $str); //过滤img标签
    $str = preg_replace("/(<align.*?>)(.*?)(<\/align>)/si", "", $str); //过滤img标签
    $str = preg_replace("/<(\/?b.*?)>/si", "", $str); //过滤b标签
    $str = preg_replace("/<(\/?h.*?)>/si", "", $str); //过滤b标签
    $str = preg_replace("/<(\/?a.*?)>/si", "", $str); //过滤p标签
    $str = preg_replace("/<(\/?p.*?)>/si", "", $str); //过滤p标签
    $str = preg_replace("/(<t.*?>)(.*?)(<\/t>)/si", "", $str); //过滤t标签
    $str = preg_replace("/(<url.*?>)(.*?)(<\/url>)/si", "", $str); //过滤img标签
    $str = preg_replace("/<(\/?font.*?)>/si", "", $str); //过滤font标签
    $str = preg_replace("/<(\/?span.*?)>/si", "", $str); //过滤font标签
    $str = preg_replace("/<(\/?strong.*?)>/si", "", $str); //过滤font标签
    $str = preg_replace("/<(\/?div.*?)>/si", "", $str); //过滤div标签
    $str = preg_replace("/<(\/?div.*?)>/si", "", $str); //过滤div标签
    $str = preg_replace("/<(\/?ul.*?)>/si", "", $str); //过滤div标签
    $str = preg_replace("/<(\/?cite.*?)>/si", "", $str); //过滤div标签
    $str = preg_replace("/<(\/?li.*?)>/si", "", $str); //过滤div标签
    $str = preg_replace("/cookie/si", "COOKIE", $str); //过滤COOKIE标签

    $str = preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si", "", $str); //过滤applet标签
    $str = preg_replace("/<(\/?applet.*?)>/si", "", $str); //过滤applet标签

    $str = preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si", "", $str); //过滤style标签
    $str = preg_replace("/<(\/?style.*?)>/si", "", $str); //过滤style标签

    $str = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si", "", $str); //过滤title标签
    $str = preg_replace("/<(\/?title.*?)>/si", "", $str); //过滤title标签

    $str = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si", "", $str); //过滤object标签
    $str = preg_replace("/<(\/?objec.*?)>/si", "", $str); //过滤object标签

    $str = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si", "", $str); //过滤noframes标签
    $str = preg_replace("/<(\/?noframes.*?)>/si", "", $str); //过滤noframes标签

    $str = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si", "", $str); //过滤frame标签
    $str = preg_replace("/<(\/?i?frame.*?)>/si", "", $str); //过滤frame标签

    $str = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $str); //过滤script标签
    $str = preg_replace("/<(\/?script.*?)>/si", "", $str); //过滤script标签
    $str = preg_replace("/javascript/si", "Javascript", $str); //过滤script标签
    $str = preg_replace("/vbscript/si", "Vbscript", $str); //过滤script标签
    $str = preg_replace("/on([a-z]+)\s*=/si", "On\\1=", $str); //过滤script标签
    $str = preg_replace("/&#/si", "&＃", $str); //过滤script标签，如javAsCript:alert(

    return $str;
}

